package com.lovo.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.stock.bean.PurchaseBean;
import com.lovo.stock.bean.PurdetailBean;
import com.lovo.stock.mapper.PurdetailMapper;
import com.lovo.stock.service.IPurchaseService;
import com.lovo.stock.service.IPurdetailService;
import net.sf.jsqlparser.statement.create.table.Index;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PurdetailServiceImpl extends ServiceImpl<PurdetailMapper, PurdetailBean> implements IPurdetailService {
    @Autowired
    PurdetailMapper purdetailMapper;
    @Autowired
    IPurchaseService purchaseService;

    @Override
    public List<PurdetailBean> queryPurdetailById(int purchaseId) {
        QueryWrapper q=new QueryWrapper<>();
        q.eq("purdetail_pid",purchaseId);
        return purdetailMapper.selectList(q);
    }

    @Override
    @Transactional
    public void addPurdetail(PurchaseBean purchaseBean) {
        //将自动生成的主键赋予即将加入数据库的明细表信息
        int id=purchaseService.addPurchase(purchaseBean);
        for (PurdetailBean p:purchaseBean.getPurdetails()
             ) {
            p.setPurdetailPid(id);
        }
        //将数据库明细表信息填入数据库
        purdetailMapper.addPurdetail(purchaseBean.getPurdetails());
    }
}
